package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.Language
import com.youdianstar.app.R
import com.youdianstar.app.entity.PointRecord
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.util.DateUtil
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.common.util.LogUtil
import com.youdianstar.app.util.DataHelperEx


/**
 * File: PointsPagingAdapter
 * Author: 82149 Create: 2022/3/2 17:29
 * Changes (from 2022/3/2)
 * --------------------------------------------------
 * description: dataType 1-增加 2-使用
 * ---------------------------------------------------
 *
 */
class PointsPagingAdapter(val mContext: Context, val dataType: Int) : PagingDataAdapter<PointRecord, PointsPagingAdapter.PointHolder>(
    differCallback
){
    companion object {
        val TAG  = "PointsPagingAdapter"
        val differCallback = object : DiffUtil.ItemCallback<PointRecord>() {
            override fun areItemsTheSame(oldItem: PointRecord, newItem: PointRecord): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: PointRecord, newItem: PointRecord): Boolean {
                return oldItem == newItem
            }
        }
    }

    private var languageNow :String = ""
    var symbolStr = ""
    init {
        languageNow = LanguageConfig.instance.getLanguageCode()
        symbolStr = DataAppManager.instance.symbolTx()
    }

    class PointHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        //val container = itemView.findViewById<ConstraintLayout>(R.id.cl_container_record)
        val tvTitle = itemView.findViewById<TextView>(R.id.item_title)
        val tvDate = itemView.findViewById<TextView>(R.id.item_date)
        val tvPoints = itemView.findViewById<TextView>(R.id.item_points)
        val tvNowValue = itemView.findViewById<TextView>(R.id.item_now)
    }

    override fun onBindViewHolder(holder: PointHolder, position: Int) {
        getItem(position)?.let { rc->
            var titleStr = ""
            if (dataType == 1){
                titleStr = when(languageNow){
                    Language.ES->{
                        rc.title?.es?:"--"
                    }

                    Language.ZH->{
                        rc.title?.zh ?: "--"
                    }

                    else ->{
                        rc.title?.en?:"--"
                    }
                }
            }else{
                //使用积分: 7-兑换优惠券 8-兑换礼物
                titleStr = if (null!=rc.couponInfo){
                    if (null!=rc.couponInfo.deduction)
                        DataHelperEx.couponDeductTx(mContext,symbolStr,rc.couponInfo.deduction)
                    else
                        "--"
                }else{
                    when(rc.type){
                        7-> mContext.getString(R.string.points_redeemCoupon)
                        else ->{
                            "--"
                        }
                    }
                }
            }
            LogUtil.d("----rc : "+titleStr)
            holder.tvTitle?.text = titleStr
            holder.tvDate?.text = if (null==rc.createTime) "--" else DateUtil.getOrderTime(rc.createTime)
            // 1-增加 2-使用
            val contentStr = if (dataType==1) "+"+rc.point.toString() else rc.point.toString()
            holder.tvPoints.text = contentStr
            holder.tvNowValue.text = rc.currentPoint.toString()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PointHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_point_record, parent, false)
        return PointHolder(view)
    }
}